package com.epam.task.thread;

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: zhunusbekova.arailym
 * Date: 05.09.13
 * Time: 14:23
 * To change this template use File | Settings | File Templates.
 */
public class Client implements Runnable {
    public static final Logger logger = Logger.getLogger(Client.class);
    private OperatorPool queue;
    private String name;

    public Client(String name, OperatorPool queue) {
        this.queue = queue;
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            Operator operator = queue.takeOperator();
            process(operator);
        }
    }

    private void process(Operator operator) {
        try {
            TimeUnit.SECONDS.sleep(operator.getDuration());
            logger.info(String.format(" %s  \"%s\" in %d seconds",
                    name,
                    operator.getDescription(),
                    operator.getDuration()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
